using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;
using libsecondlife.Packets;

using Feathertail;

namespace NestBox.Inventory
{
    class detach : Feathertail.Command
    {
        private FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length != 2)
            {
                return GetHelpText();
            }

            string item2detach = CmdLine[1];
            string output = "";


            // Iterate through current folder contents to find item
            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

            output += "Looking for : " + item2detach + Environment.NewLine;
            iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.ItemID.Equals(item2detach) || item.Name.Equals(item2detach))
                    {
                        output += "Attempting to detach " + item.Name;
                        item.Detach();
                    }
                }
            }
            return output;
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "detach" };
        }

        public string GetSummaryText()
        {
            return "Attempt to remove an attachment (back into inventory)";
        }

        public string GetHelpText()
        {
            return "detech [item]";
        }

        #endregion
    }
}
